package com.codinghero.zqlb.audit.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.codinghero.util.StringUtils;
import com.codinghero.zqlb.match.SoccerMatch;
import com.codinghero.zqlb.spider.bo.MatchBo;

public class AuditMatchServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private MatchBo matchBo = new MatchBo();

	public String updateMatchStatusByIds(HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		String[] ids = req.getParameterValues("id");
		int status = Integer.valueOf(req.getParameter("status"));
		matchBo.updateMatchStatusByIds(ids, status);
		this.list(req, resp);
		return "/audit/match?token=" + req.getParameter("token");
	}

	public String list(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<SoccerMatch> list = matchBo.getAllMatches();
		req.setAttribute("list", list);
		return "/admin/audit/match_list.jsp?token=" + req.getParameter("token");
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method = req.getParameter("method");
		if (StringUtils.isEmpty(method)) {
			method = "list";
		}

		if (method.equals("list")) {
			String jsp = this.list(req, resp);
			req.getRequestDispatcher(jsp).forward(req, resp);
		} else if (method.equals("updateMatchStatusByIds")) {
			String jsp = this.updateMatchStatusByIds(req, resp);
			resp.sendRedirect(jsp);
		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
}
